<template>
    <div class="page">
        <AdminTabsP :list="tabList" v-model:active="tabActive" />
        <AdminSearchInputP @search="onSearch" />

        <template v-if="!isLoading">
            <div class="work-list" v-if="list.length">
                <div class="work-item" v-for="item in list" :key="item.id">
                    <img class="work-item__img" :src="item.file +
                        '?x-oss-process=image/watermark,image_' +
                        toBase64(
                            'water/logo_white.png?x-oss-process=image/resize,P_30'
                        ) +
                        ',g_center,t_60'
                        " />
                    <div class="work-item__info">
                        <div class="work-info__top">
                            <div class="work-info__title">
                                {{ item.name }}
                            </div>
                            <div class="work-info__btn_group">
                                <div class="btn" @click="onEditContent(item, tabActive)">修改</div>
                                <span class="btn" @click="onShare(item)">
                                    分享
                                </span>
                                <span class="btn" @click="onJumpUrl(item)">
                                    预览
                                </span>
                                <span class="btn" @click="onEdit(item)">
                                    更新
                                </span>
                                <el-popconfirm title="是否确认删除？" @confirm="onDelWork(item)">
                                    <template #reference>
                                        <span class="btn"> 删除 </span>
                                    </template>
                                </el-popconfirm>
                            </div>
                        </div>

                        <div class="work-info__bottom">
                            <div class="work-info__group">
                                <img :src="`${_oss_}images/admin/works/time.png`" />
                                {{ item.createtime }}
                            </div>
                            <div class="work-info__group is-view">
                                <img :src="`${_oss_}images/works/icon-view.png`" />
                                {{ item.page_view }}
                            </div>
                            <div class="work-info__group is-like" :class="{ active: item.is_like }" @click="onLike(item)">
                                <img :src="item.is_like
                                    ? `${_oss_}images/works/icon-like-active.png`
                                    : `${_oss_}images/works/icon-like.png`
                                    " />
                                {{ item.like }}
                            </div>
                            <div class="work-info__group is-star" :class="{ active: item.is_star }" @click="onStar(item)">
                                <img :src="item.is_star
                                    ? `${_oss_}images/works/icon-star-active.png`
                                    : `${_oss_}images/works/icon-star.png`
                                    " />
                                {{ item.star }}
                            </div>
                            <div class="work-info__group is-comment" @click="onComment(item)">
                                <img :src="`${_oss_}images/works/icon-comment.png`" />
                                {{ item.comment }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <el-empty :image-size="150" v-else />
        </template>
    </div>

    <DialogEditWorkP ref="dialogEditWorkRef" @success="onSuccess" />
</template>
<script setup>
import { useContent } from ".";
import { useLike, useStar } from "~/hooks";

const dialogEditWorkRef = ref(null);

const {
    tabActive,
    tabList,
    isLoading,
    list,
    onJumpUrl,
    onEdit,
    onShare,
    onDelWork,
    onSearch,
    onComment,
    GetList,
} = useContent();
const { onLike } = useLike();
const { onStar } = useStar();

const onSuccess = () => {
    GetList(false);
};

const onEditContent = (item, type) => {
    dialogEditWorkRef.value.show(item, type);
}

await GetList();
</script>
<style lang="scss" scoped>
.page {
    width: 100%;
    padding: 0 16px;
}

.work-list {
    height: calc(100% - 145px);
    overflow: auto;

    .work-item {
        display: flex;
        padding: 12px;
        border-bottom: 1px solid #ddd;
        background-color: #fff;

        &__img {
            flex-shrink: 0;
            width: 100px;
            height: 68px;
            object-fit: cover;
            border-radius: 6px;
            flex-shrink: 0;
        }

        &__info {
            width: 100%;
            height: 65px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            padding-top: 10px;
            padding-left: 17px;
        }

        .work-info__top {
            display: flex;
            align-items: center;

            .work-info__btn_group {
                display: flex;
                align-items: center;
                margin-left: auto;
                color: #5185fb;
                font-size: 14px;

                .btn {
                    margin-left: 20px;
                    user-select: none;
                    cursor: pointer;
                }
            }
        }

        .work-info__bottom {
            display: flex;
            align-items: center;
            margin-top: auto;
            color: #666;

            .work-info__group {
                display: flex;
                align-items: center;
                margin-right: 40px;

                &.is-like,
                &.is-star,
                &.is-comment {
                    cursor: pointer;
                }

                &.is-like,
                &.is-star {
                    img {
                        transition: all 0.2s;
                    }

                    &:active img {
                        transform: scale(1.3);
                    }
                }

                img {
                    width: 14px;
                    margin-right: 4px;
                }
            }
        }
    }
}
</style>
